import pygame
import math
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))

# 无人机
class Plane(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image_og = pygame.transform.smoothscale(
            pygame.image.load("image/plane.png"), (25, 25)
        )  # 保留一份原版图像，之后的旋转基于此原版图像，防止旋转次数过多卡死
        self.image = self.image_og
        self.rect = self.image.get_rect()
        self.v = 5  # 速度，控制每步位移长度
    
    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def step(self, angle, screen_size):
        # angle  float  [-pi, pi]  0:右，pi/2:下

        # 移动
        self.rect.centerx += self.v * math.cos(angle)
        self.rect.centery += self.v * math.sin(angle)
        
        # 边缘控制
        self.rect.centerx = min(max(self.rect.centerx, 0), screen_size[0] - 1)
        self.rect.centery = min(max(self.rect.centery, 0), screen_size[1] - 1)

        # 旋转
        degree = -angle * 180 / math.pi
        self.image = pygame.transform.rotate(self.image_og, degree)
        self.rect = self.image.get_rect(center=self.rect.center)
